package com.xu.tools.mapstruct;

import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author xuguan
 * @since 2024/4/1
 */
// componentModel = "spring" 属性可以给生成的实体类添加@Component注解, 注入ioc容器
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface UserConverter {

    // 寻找自动生成的UserConverterImpl类, 如果使用spring ioc容器注入省略
    UserConverter INSTANCE = Mappers.getMapper(UserConverter.class);

    @Mapping(target = "sex", source = "gender")
    @Mapping(target = "home", source = "address")
    @Mapping(target = "password", ignore = true)
    @Mapping(target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "createTime", dateFormat = "yyyy-MM-dd HH:mm:ss")
    @Mapping(target = "salary", numberFormat = "#.00", defaultValue = "0.00")
    UserVO userDtoToUserVo(UserDTO userDto);

    // 直接继承并反转Mapping配置
    @InheritInverseConfiguration(name = "userDtoToUserVo")
    UserDTO userVoToUserDto(UserVO userDto);

    List<UserVO> userDtosToUserVos(List<UserDTO> userDtos);

    List<UserDTO> userVosToUserDtos(List<UserVO> userVos);
}